package com.hosp.controller.v1;


import java.util.List;

import com.github.pagehelper.PageInfo;
import com.hosp.bean.Doctor;
import com.hosp.service.DoctorService;
import com.hosp.vo.PageRequestVO;
import com.hosp.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 医生管理 API
 *
 * @author bing.zhang06@hand-china.com 2021/10/02 1:07
 **/
@Slf4j
@RestController
@RequestMapping("/doctor")
public class DoctorController {
    @Autowired
    private DoctorService doctorService;

    /**
     * 查询列表
     *
     * @param doctor      查询条件
     * @param pageRequest 分页条件
     * @return 响应
     */
    @GetMapping("/list")
    public ResultVO<Doctor> list(Doctor doctor, PageRequestVO pageRequest) {
        PageInfo<Doctor> pageInfo = doctorService.list(doctor, pageRequest);
        return new ResultVO<>(pageInfo);
    }

    /**
     * 主键查询
     *
     * @param dId 查询条件
     * @return 响应
     */
    @GetMapping("/{dId}")
    public ResultVO<Doctor> selectById(@PathVariable("dId") Integer dId) {
        Doctor doctor = doctorService.selectById(dId);
        return new ResultVO<>(doctor);
    }

    /**
     * 根据科室查询医生列表
     *
     * @param keshi 查询条件
     * @return 响应
     */
    @GetMapping("/ks/{keshi}")
    public ResultVO<Doctor> selectByKs(@PathVariable("keshi") String keshi) {
        List<Doctor> doctors = doctorService.selectByKs(keshi);
        return new ResultVO<>(doctors);
    }

    /**
     * 保存
     *
     * @param doctor 保存对象
     * @return 响应
     */
    @PostMapping("/save")
    public ResultVO<String> save(Doctor doctor) {
        log.debug("Doctor is {}.", doctor);

        boolean flag = doctorService.save(doctor);

        if (flag) {
            return new ResultVO<>(200, "保存成功");
        } else {
            return new ResultVO<>(500, "保存失败");
        }
    }

}
